package com.example.simpletask

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.cardview.widget.CardView
import androidx.recyclerview.widget.RecyclerView

class StepsSelectorAdapter(private var steps: Int) :
    RecyclerView.Adapter<StepsSelectorAdapter.ViewHolder>() {

    inner class ViewHolder(view: View): RecyclerView.ViewHolder(view) {
        fun setColor(position: Int, steps: Int) {
            val color = itemView.findViewById<CardView>(R.id.centerColor)
            if (position <= steps) {
                color.setCardBackgroundColor(BgColor.get(steps, position))
            } else {
                color.setCardBackgroundColor(itemView.resources.getColor(R.color.selector_normal))
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.selector_item, parent, false)

        val viewHolder = ViewHolder(view)
        viewHolder.itemView.setOnClickListener {
            val position = viewHolder.adapterPosition
            if (position > 0) {
                steps = position
                notifyDataSetChanged()
            }
        }
        return viewHolder
    }

    override fun getItemCount(): Int = 7

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.setColor(position, steps)
    }

    fun getSteps(): Int = steps
}